package com.yict.csms.resourceplan.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yict.common.Constants;
import com.yict.common.service.impl.CommonService;
import com.yict.common.util.DateUtil;
import com.yict.csms.resourceplan.dao.impl.RtgcFlPlanAllDaoImpl;
import com.yict.csms.resourceplan.dao.impl.RtgcFlTimeDaoImpl;
import com.yict.csms.resourceplan.dao.impl.RtgcFlWharfOptionDaoImpl;
import com.yict.csms.resourceplan.dao.impl.RtgcFlWhartNeedDaoImpl;
import com.yict.csms.resourceplan.entity.RtgcFlPlan;
import com.yict.csms.resourceplan.entity.RtgcFlPlanAll;
import com.yict.csms.resourceplan.entity.RtgcFlTime;
import com.yict.csms.resourceplan.entity.RtgcFlWharfOption;
import com.yict.csms.resourceplan.entity.RtgcFlWhartNeed;
import com.yict.csms.resourceplan.entity.RtgcFlWhartNeedHour;
import com.yict.csms.system.entity.DataDict;
import com.yict.csms.system.entity.User;

@Service
public class RtgcFlWharfOptionServiceImpl extends
		CommonService<RtgcFlWharfOption, Long> {

	@Autowired
	private RtgcFlWharfOptionDaoImpl rtgcFlWharfOptionDaoImpl;
	@Autowired
	private RtgcFlWhartNeedDaoImpl rtgcFlWhartNeedDaoImpl;
	@Autowired
	private RtgcFlTimeDaoImpl rtgcFlTimeDaoImpl;
	@Autowired
	private RtgcFlPlanAllDaoImpl rtgcFlPlanAllDaoImpl;
	
	public List<RtgcFlWharfOption> search(Map<String, Object> queryMap)
			throws Exception {
		StringBuilder queryString = new StringBuilder();
		List<RtgcFlWharfOption> list = new ArrayList<RtgcFlWharfOption>();
		try {
			queryString.append("from RtgcFlWharfOption as c where 1=1 ");
			if (queryMap != null && queryMap.size() > 0) {
				if (queryMap.get("dictid") != null
						&& !"".equals(queryMap.get("dictid"))) {
					queryString.append(" and c.dataDict.dictid = :dictid");
				}
				if (queryMap.get("timeId") != null
						&& !"".equals(queryMap.get("timeId"))) {
					queryString.append(" and c.rtgcFlTime.timeid = :timeId");
				}
			}
			list = rtgcFlWharfOptionDaoImpl.search(queryString.toString(),
					queryMap, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Transactional(readOnly = false,rollbackFor = {Exception.class})
	public boolean saveOrUpdate(RtgcFlTime rtgcFlTime,
			List<RtgcFlWharfOption> optionList, List<RtgcFlWhartNeed> needList,
			List<RtgcFlWhartNeedHour> needHourList) {
		boolean bool = false;
		try {
			bool = rtgcFlTimeDaoImpl.saveOrUpdate(rtgcFlTime);
			bool = rtgcFlWharfOptionDaoImpl.saveOrUpdate(optionList);
			bool = rtgcFlWhartNeedDaoImpl.saveOrUpdate(needList);
			bool = rtgcFlWhartNeedDaoImpl.saveOrUpdateByNeedHour(needHourList);
		} catch (Exception e) {
			bool = false;
			e.printStackTrace();
			throw new RuntimeException();
		}
		return bool;
	}

	@Transactional(readOnly = false,rollbackFor = {Exception.class})
	public boolean commit(String timeId, HashMap<String, DataDict> dictMap,
			User user) {
		boolean bool = false;
		try {
			RtgcFlTime rtgcFlTime = rtgcFlTimeDaoImpl
			.findbyId(Long.valueOf(timeId));
			List<RtgcFlWhartNeedHour> needHour = rtgcFlWhartNeedDaoImpl.findByNeedHourTimeId(timeId);
			rtgcFlTime.setStatus(dictMap.get("status2"));
			rtgcFlTime.setCommitdate(new Date());
			RtgcFlPlanAll planAll = new RtgcFlPlanAll();
			planAll.setRtgcFlTime(rtgcFlTime);
			planAll.setPlanDate(rtgcFlTime.getRtgcfldate());
			planAll.setPlanTime(rtgcFlTime.getRtgcfltime());
			planAll.setMachine(dictMap.get("machine"));
			planAll.setStatus(dictMap.get("status1"));
			planAll.setCreateperson(user.getUserId());
			planAll.setCreatedate(new Date());
			planAll.setUpdateperson(user.getUserId());
			planAll.setUpdatedate(new Date());
			
			List<RtgcFlPlan> list = new ArrayList<RtgcFlPlan>();
			double[] timeSegments = changToTimeSegments(needHour);
			int k= 0;
			double bfValue = 0.0;
			for (int i = 0; i < timeSegments.length; i++) {
				RtgcFlPlan plan = new RtgcFlPlan();
				plan.setRtgcFlPlanAll(planAll);
				plan.settMode(dictMap.get("tMode"));
				plan.setCreateperson(user.getUserId());
				plan.setCreatedate(new Date());
				plan.setUpdateperson(user.getUserId());
				plan.setUpdatedate(new Date());
				if(i>0){
				k=i;
				bfValue = timeSegments[i-1];
				}
				if(!(timeSegments[i]==bfValue)){
					double value = timeSegments[i] - bfValue;
					plan.setAddorcut("加");
					if (value < 0) {
						plan.setAddorcut("减");
					}
					String[] quTimes = Constants.quTime;
					int quTimesFlag = 0;
					if (rtgcFlTime.getRtgcfltime().getDatacode()
							.equals("C_time_bucket"))
						quTimesFlag = 0;
					if (rtgcFlTime.getRtgcfltime().getDatacode()
							.equals("A_time_bucket"))
						quTimesFlag = 16;
					if (rtgcFlTime.getRtgcfltime().getDatacode()
							.equals("B_time_bucket"))
						quTimesFlag = 32;
					String flDate = DateUtil.DateToString(
							rtgcFlTime.getRtgcfldate(), 1)
							+ " " + quTimes[quTimesFlag + k];
					plan.setOperatTime(DateUtil.StringToDate(flDate, 5));
					plan.setChangeQuantity(value);	
					plan.setHours(8-i/2d);
					plan.setQuanHours(value * (8-i/2d));
					list.add(plan);
				}
			}
			bool = rtgcFlTimeDaoImpl.update(rtgcFlTime);
			bool = rtgcFlPlanAllDaoImpl.saveOrUpdate(planAll);
			bool = rtgcFlPlanAllDaoImpl.saveOrUpdateRtgcFlPlan(list);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return bool;
	}

	private double[] changToTimeSegments(List<RtgcFlWhartNeedHour> needHourList) {
		double[] timeSegments = new double[16];
		timeSegments[0] = 0.0;
		for (RtgcFlWhartNeedHour needHour : needHourList) {
			timeSegments[0] += needHour.getTimeSegment1();
			timeSegments[1] += needHour.getTimeSegment2();
			timeSegments[2] += needHour.getTimeSegment3();
			timeSegments[3] += needHour.getTimeSegment4();
			timeSegments[4] += needHour.getTimeSegment5();
			timeSegments[5] += needHour.getTimeSegment6();
			timeSegments[6] += needHour.getTimeSegment7();
			timeSegments[7] += needHour.getTimeSegment8();
			timeSegments[8] += needHour.getTimeSegment9();
			timeSegments[9] += needHour.getTimeSegment10();
			timeSegments[10] += needHour.getTimeSegment11();
			timeSegments[11] += needHour.getTimeSegment12();
			timeSegments[12] += needHour.getTimeSegment13();
			timeSegments[13] += needHour.getTimeSegment14();
			timeSegments[14] += needHour.getTimeSegment15();
			timeSegments[15] += needHour.getTimeSegment16();
		}
		return timeSegments;
	}
}
